package com.cyy.activitylifecycleyanalysisdemo.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.cyy.activitylifecycleyanalysisdemo.adapter.Class2Desc;
import com.cyy.activitylifecycleyanalysisdemo.adapter.IndexActivityAdapter;
import com.cyy.activitylifecycleyanalysisdemo.adapter.RecyclerViewSimpleAdapter;
import com.cyy.activitylifecycleyanalysisdemo.utils.LogUtils;
import com.cyy.activitylifecycleyanalysisdemo.R;

import java.util.ArrayList;
import java.util.List;

public class IndexActivity extends AppCompatActivity {

    private static final String TAG = LogUtils.getClassTag(IndexActivity.class);
    private RecyclerView rlist_index;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        rlist_index = findViewById(R.id.rlist_index);
        List<Class2Desc> activities = getIndexActivitiesName();
        rlist_index.setLayoutManager(
                new LinearLayoutManager(getBaseContext(), LinearLayoutManager.VERTICAL, false));
        rlist_index.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        rlist_index.setAdapter(new IndexActivityAdapter(activities,
                new RecyclerViewSimpleAdapter.OnItemClickListener<Class2Desc>() {
            @Override
            public void onClick(View view, int position, Class2Desc data) {
                try {
                    Class clazz = Class.forName(data.getClassName());
                    Intent intent = new Intent(getBaseContext(), clazz);
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }));
    }

    private List<Class2Desc> getIndexActivitiesName(){
        ArrayList<Class2Desc> activitiesName = new ArrayList<>();
        activitiesName.add(new Class2Desc(MainActivity.class.getName(), "Activity的生命周期"));
        activitiesName.add(new Class2Desc(HasFragmentActivity.class.getName(), "Activity和Fragment的生命周期"));
        return activitiesName;
    }
}
